package com.wonderful.map

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.PointF
import android.util.AttributeSet
import androidx.annotation.DrawableRes
import com.wonderful.image.ScaleImageView
import kotlin.math.roundToInt

/**
 * Desc: Pin view
 * @Author: wonderful
 * @Time: 2023/5/10 10:49
 */
class PinView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ScaleImageView(context, attrs) {

    companion object {
        const val SHOW_TYPE_CENTER = 1  // 显示点上面
        const val SHOW_TYPE_TOP = 2     // 显示点中间
    }

    private val mPaint = Paint()
    private val vPin = PointF()
    private val sPin = PointF()

    private var pin: Bitmap
    private var pinSize: Int

    private var isShow: Boolean = true
    private var showType: Int = SHOW_TYPE_CENTER

    init {
        mPaint.isAntiAlias = true

        // 初始化默认大小
        val density = resources.displayMetrics.density
        pinSize = (30 * density + 0.5f).roundToInt()

        // 初始化默认图片
        pin = BitmapFactory.decodeResource(resources, R.drawable.ic_direction_nv)
        setPinImage(pin)
    }

    override fun onReady() {
        // 点坐标未设置时默认中心点
        if (sPin.x == 0f && sPin.y == 0f) {
            center?.let { sPin.set(it) }
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (isShow && isReady) {
            sourceToViewCoord(sPin, vPin)
            val vX = vPin.x - (pin.width / 2)
            val vY = if (showType == SHOW_TYPE_TOP) {
                // 点之上
                vPin.y - pin.height
            } else {
                // 点中间
                vPin.y - (pin.height / 2)
            }
            canvas?.drawBitmap(pin, vX, vY, mPaint)
        }
    }

    /**
     * 设置 pin 图片
     */
    fun setPinImage(bitmap: Bitmap) {
        //val density = resources.displayMetrics.densityDpi
        //val w = (density / 420f * bitmap.width).roundToInt()
        //val h = (density / 420f * bitmap.height).roundToInt()
        //pin = Bitmap.createScaledBitmap(bitmap, w, h, true)
        val w: Int
        val h: Int
        if (bitmap.width > bitmap.height) {
            w = pinSize * (bitmap.width / bitmap.height)
            h = pinSize
        } else {
            w = pinSize
            h = pinSize * (bitmap.height / bitmap.width)
        }
        pin = Bitmap.createScaledBitmap(bitmap, w, h, true)
        invalidate()
    }

    /**
     * 设置 pin 图片
     */
    fun setPinImage(@DrawableRes imgId: Int) {
        pin = BitmapFactory.decodeResource(resources, imgId)
        setPinImage(pin)
    }

    /**
     * 设置是否显示 pin
     */
    fun setPinShow(isShow: Boolean) {
        this.isShow = isShow
        invalidate()
    }

    /**
     * 设置 pin 大小
     */
    fun setPinSize(sizeDp: Float) {
        if (sizeDp <= 0) return
        val density = resources.displayMetrics.density
        pinSize = (sizeDp * density + 0.5f).roundToInt()
        invalidate()
    }

    /**
     * 设置 pin 位置
     */
    fun setPinPointF(sPin: PointF) {
        this.sPin.set(sPin)
        invalidate()
    }

    /**
     * 设置 pin 角度
     */
    fun setPinAngle(angle: Float) {
        val matrix = Matrix()
        matrix.postRotate(angle)

        val resizedBitmap = Bitmap.createBitmap(pin, 0, 0, pin.width, pin.height, matrix, true)
        if (resizedBitmap != pin && !pin.isRecycled) {
            pin.recycle()
            pin = resizedBitmap
        }
        invalidate()
    }

    /**
     * 设置 pin 显示方式
     */
    fun setPinShowType(type: Int) {
        showType = if (type == SHOW_TYPE_TOP) {
            SHOW_TYPE_TOP
        } else {
            SHOW_TYPE_CENTER
        }
        invalidate()
    }
}